/*#include <msp430g2231.h>

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;         // Stop watchdog timer
  P1DIR=0x41;
 while(1)
 {
  P1OUT = 0x01; 		  // green LED on
 // _delay_cycles(50000);                
 // P1OUT ^= 0x40; 	         // red LED on   	  
 // _delay_cycles(50000);
 }
}*/

#include <msp430g2231.h>
#define RED    (1<<0)             // Red LED is at bit 0 of Port 1
#define GREEN  (1<<6)             // Green LED is at bit 6 of Port 1
#define BUTTON (1<<3)             // Push Button is at bit 3 of Port 1
int ms, SEC, MIN ;
void SystemInit(void);

void main(){
      
       SystemInit();

       while( (P1IN&BUTTON)!=0 ); // Wait here if button not pressed
       while( (P1IN&BUTTON)==0 ); // Wait here till button is released

       TACCR0 = 999 ;                                  // 1000/1000000 = 1 ms
       TACTL = TASSEL1 + MC0 + TAIE  ;   // SMCLK, UP, Interrupt enabled 
       ms = 0 ;
       SEC = 0 ;
       MIN = 0;
       _enable_interrupts();                    // or use _BIS_SR(GIE);  see SLAU132G
      
       while(1){;}          // Wait for interrupts
      
}

#pragma vector=TIMERA1_VECTOR            // at 0xFFF0
__interrupt void TimerAISR(void){
       if(TAIV==TAIV_TAIFG){                    // Using TAIFG (Timer overflow)
              ms++ ;
              if(ms==1000){
                     ms = 0 ;
                     SEC++;
                        if(MIN==60) SEC = 0 ;
                     if(SEC==60){SEC = 0 ; MIN++;}
                     if((SEC%2)==0)P1OUT &= ~RED ;     // Even, Red off
                                  else  P1OUT |=  RED ;      // Odd, Red ON      
                     if((MIN%2)==0)    P1OUT &= ~GREEN ;    // 0-4, Green off
                           else    P1OUT |=  GREEN ;  // 5-9, Green ON          
              }
       }     
}

void SystemInit(void){
       // Stop watchdog timer
       WDTCTL = WDTPW + WDTHOLD; 
       // GPIO Configuration for bit 0 Red LED
       P1SEL &= ~RED ;      // 0 for I/O function
       P1DIR |=  RED ;      // 1 for output
       P1OUT &= ~RED ;      // Clear the bit
       // GPIO Configuration for bit 6 Green LED
       P1SEL &= ~GREEN ;    // 0 for I/O function
       P1DIR |=  GREEN ;    // 1 for output
       P1OUT &= ~GREEN ;    // Clear the bit
       // GPIO Configuration for bit 3 Push Button
       P1SEL &= ~BUTTON ;   // 0 for I/O function
       P1DIR &= ~BUTTON ;   // 0 for input
       P1REN |=  BUTTON ;   // 1 to enable PUPD
       P1OUT |=  BUTTON ;   // 1 to Pull Up
}

